﻿<#include "../../../common/_meta.ftl">
<link rel="stylesheet" href="${springMacroRequestContext.contextPath}/static/lib/layui-v2.5.6/layui/css/layui.css" media="all">
<title>角色组管理</title>
</head>
<body>
<nav class="breadcrumb">
	<!--页面导航栏-->
	<span class="layui-breadcrumb" lay-separator="-"></span>
	<a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="Hui-iconfont">&#xe68f;</i></a>
</nav>
<div class="page-container">
	<div class="cl pd-5 bg-1 bk-gray">

		<div class="search-param">
			<div class="layui-inline">
				<input type="hidden" id="parentid" name="parentid" value="${parentid}" placeholder="父id" >
				<input type="text" class="input-text" style="width:250px" placeholder="角色组名称" autocomplete="off" name="grolename" id="grolename">
			</div>
			<button class="btn btn-success search-btn" data-type="reload"><i class="Hui-iconfont">&#xe665;</i> 搜索</button>
		</div>
		<!--表格-->
		<table class="table table-border table-bordered table-hover table-bg" id="dataTable" lay-filter="table-filter"></table>
		<!--头部工具栏-->
		<script type="text/html" id="headToolbar">
			<span class="l">
				<a href="javascript:" class="btn btn-danger radius" lay-event="delete"><i class="Hui-iconfont">&#xe6e2;</i> 批量删除</a>
				<a href="javascript:" class="btn btn-primary radius" lay-event="add"><i class="Hui-iconfont">&#xe600;</i> 添加角色组</a>
				<a href="javascript:" class="btn btn-secondary radius" lay-event="upLevel"><i class="Hui-iconfont">&#xe679;</i> 返回上层</a>
			</span>
		</script>
		<!--行工具栏-->
		<script type="text/html" id="lineBar">
			<#--
			<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
			<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="upLevel">上级</a>
			-->
			<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="downLevel">下级</a>
			<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
			<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
		</script>
</div>
<!--_footer 作为公共模版分离出去-->
<#include "../../../common/_footer.ftl">
<!--/_footer 作为公共模版分离出去-->
<script type="text/javascript" src="${springMacroRequestContext.contextPath}/static/lib/layui-v2.5.6/layui/layui.js"></script>
<!--页面导航栏-->
<script type="text/javascript" src="${springMacroRequestContext.contextPath}/static/custom/js/element.js"></script>
<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript">

$(function () {
	<!--页面导航栏-->
	breadcrumb(${menuid});
})
//新增编辑页面url
let editUrl = contextPath + '/admin/rolegroupEdit';
//全局变量table
let table;
layui.use(['table','element'], function(){
	table = layui.table;
	let element = layui.element;
	table.render({
		id: 'table_id'
		,elem: '#dataTable'
		,toolbar: '#headToolbar' //开启头部工具栏，并为其绑定左侧模板
		,title: '角色组表'
		,height: 'full-150'
		,method:"get"
		// ,contentType:"application/json"
		,url: contextPath+"/adminApi/rolegroup/" //数据接口
		,response: {
			statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
		}
		,parseData: function(res){ //res 即为原始返回的数据
			return {
				"code": res.code, //解析接口状态
				"msg": res.message, //解析提示文本
				"count": res.data.total, //解析数据长度
				"data": res.data.list //解析数据列表
			};
		}
		,request: {
			pageName: 'pageNum' //页码的参数名称，默认：page
			,limitName: 'pageSize' //每页数据量的参数名，默认：limit
		}
		,page: true //开启分页
		,cols: [[ //表头
			{type: 'checkbox', fixed: 'left',field: 'groleid',width:'10%' }
			,{field: 'grolename', title: '角色组名', width:'20%' , sort: true}
			,{field: 'info', title: '描述', width:'50%', sort: true, totalRow: false}
			,{fixed: 'right',title: '操作', width:'20%', align:'center', toolbar: '#lineBar'}
		]]
	});

	//监听头工具栏事件
	table.on('toolbar(table-filter)', function(obj){
		let checkStatus = table.checkStatus(obj.config.id)
				,data = checkStatus.data; //获取选中的数据
		switch(obj.event){
			case 'add':
				admin_rolegroup_add('添加角色组',editUrl+'?parentid='+$('#parentid').val()+'&groleid=','800');
				break;
			case 'update':
				if(data.length === 0){
					layer.msg('请选择一行');
				} else if(data.length > 1){
					layer.msg('只能同时编辑一个');
				} else {
					layer.alert('编辑 [id]：'+ checkStatus.data[0].groleid);
				}
				break;
			case 'delete':
				if(data.length === 0){
					layer.msg('请选择一行');
				} else {
					let data = checkStatus.data;
					let ids = [];
					for (let i = 0; i < data.length; i++) {
						ids[i] = data[i].groleid;
					}
					//layer.alert(JSON.stringify(ids));
					layer.alert(ids.join(","));
					// layer.msg('删除');
				}
				break;
			case 'upLevel':
				if ($('#parentid').val() === 'top'){
					layer.msg('已经是顶层');
				}else{
					$.ajax({
						url: contextPath+"/adminApi/rolegroup/" + $('#parentid').val(),
						type: "GET",
						dataType:"json",
						cache:false,
						async:false,
						success:function(data){
							if (data.code == 200){
								$('#parentid').val(data.data.parentid);
								reload();
							}
						},
						error:function(data){
							layer.msg('查询异常!',{icon:2,time:1000});
						}
					});

				}
				break;
		};
	});

	//监听行工具事件
	table.on('tool(table-filter)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
		let data = obj.data //获得当前行数据
				,layEvent = obj.event; //获得 lay-event 对应的值
		if(layEvent === 'downLevel'){
			$('#parentid').val(data.groleid);
			reload();
		} else if(layEvent === 'detail'){
			layer.msg('查看操作');
		} else if(layEvent === 'del'){
			layer.confirm('真的删除行么', function(index){
				layer.close(index);
				//向服务端发送删除指令
				admin_rolegroup_del(this,data.groleid,obj);
			});
		} else if(layEvent === 'edit'){
			admin_rolegroup_edit('角色组编辑', editUrl+'?parentid='+$('#parentid').val()+'&groleid='+data.groleid);
		}
	});

	let $ = layui.$, active = {
		reload: function(){
			reload();
		}
	};
	//搜索
	$('.search-param .search-btn').on('click', function(){
		let type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
})
//重新载入数据
function reload(){
	let grolename = $('#grolename');
	let parentid = $('#parentid');
	//执行重载
	table.reload('table_id', {
		page: {
			curr: 1 //重新从第 1 页开始
		}
		,where: {
			parentid: parentid.val(),
			grolename: grolename.val()
		}
	}, 'data');
}

/*管理员-角色-添加*/
function admin_rolegroup_add(title,url,w,h){
	layer_show(title,url,w,h);
}
/*管理员-角色-编辑*/
function admin_rolegroup_edit(title,url,id,w,h){
	layer_show(title,url,w,h);
}
/*管理员-角色-删除*/
function admin_rolegroup_del(obj,id,obj2){
		$.ajax({
			type: 'DELETE',
			url: contextPath+'/adminApi/rolegroup/'+id,
			dataType: 'json',
			success: function(data){
				if(data.data){//data等于true
					layer.msg('已删除!',{icon:1,time:1000});
					obj2.del(); //删除对应行（tr）的DOM结构
				}else{
					layer.msg('删除失败!',{icon:1,time:1000});
				}
			},
			error:function(data) {
				console.log(data.message);
			},
		});
}
</script>
</body>
</html>